package com.example.demo;

import org.assertj.core.util.Lists;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class VideoFrame {

    private final static String FILE_TARGET_NAME = "movie";

    /**
     * 视频文件指定时间段的帧截取及自动截取
     * @param file 视频文件
     * @param start 开始帧数
     * @param end 结束帧数
     * @param open 截取模式
     * @param filePath 图片存储路径
     * @return 图片列表
     * @throws Exception
     */
    public static List<File> videoIntercept(File file, Integer start, Integer end, Boolean open, String filePath) throws Exception {
        Frame frame = null;
        List<File> files = Lists.newArrayList();
        FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(file);
        try {
            fFmpegFrameGrabber.start();
            int ftp = fFmpegFrameGrabber.getLengthInFrames();
            System.out.println("============开始取帧============");
            System.out.println("============共:" + ftp + "帧============");
            for (int i = 0; i < ftp; i++) {
                if (open) {
                    if (i >= start && i <= end) {
                        frame = fFmpegFrameGrabber.grabImage();
                        doExecuteFrame(frame, filePath, i, files);
                    }
                } else {
                    if (i <= ftp / 10) {
                        frame = fFmpegFrameGrabber.grabImage();
                        doExecuteFrame(frame, filePath, i, files);
                    }
                    if (i > (ftp / 10) * 2 && i <= (ftp / 10) * 3) {
                        frame = fFmpegFrameGrabber.grabImage();
                        doExecuteFrame(frame, filePath, i, files);
                    }
                    if (i > (ftp / 10) * 4 && i <= (ftp / 5 / 2) * 5) {
                        frame = fFmpegFrameGrabber.grabImage();
                        doExecuteFrame(frame, filePath, i, files);
                    }
                    if (i > (ftp / 10) * 6 && i <= (ftp / 10) * 7) {
                        frame = fFmpegFrameGrabber.grabImage();
                        doExecuteFrame(frame, filePath, i, files);
                    }
                    if (i > (ftp / 10) * 8 && i <= (ftp / 10) * 10) {
                        frame = fFmpegFrameGrabber.grabImage();
                        doExecuteFrame(frame, filePath, i, files);
                    }
                }
            }
            System.out.println("============取帧结束============");
            fFmpegFrameGrabber.stop();
        } catch (IOException E) {
            throw new Exception("视频抽帧异常", E);
        }
        return files;
    }


    /**
     *
     * @param frame
     * @param targetFilePath
     * @param index
     * @param files
     */
    public static void doExecuteFrame(Frame frame, String targetFilePath, int index, List<File> files) {
        if (frame == null || frame.image == null) {
            return;
        }
        Java2DFrameConverter converter = new Java2DFrameConverter();
        String imageMat = "jpg";
//        String fileName = targetFilePath + FILE_TARGET_NAME + "_" + index + "." + imageMat;
        String fileName = targetFilePath + index + "." + imageMat;
        BufferedImage bi = converter.getBufferedImage(frame);
        File output = new File(fileName);
        files.add(output);
        try {
            ImageIO.write(bi, imageMat, output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        String filePath = "E:\\密\\zcy\\";
        String filePathVideo = "E:\\密\\视频\\xvideos.com_5bc1214d5c4eb29c4f22e1feb4a170fe.mp4";
        try {
            videoIntercept(new File(filePathVideo), 1, 200, false, filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
